package factory;

import gamehandlers.GameHandler;
import interactions.CollisionInteraction;
import interactions.Interaction;
import interactions.Reaction;
import interactions.SpritesTypePair;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import sprite.Sprite;
import spritebehavior.Behavior;


/**
 * @author guytracy
 *         creates a interaction object that contains a Hashmap of reactions
 *         this factory can parse text files that are in the form:
 *         Behavior Behavior Reaction
 */
public class InteractionFactory extends AbstractFactory {

    private Map<SpritesTypePair, Reaction> myReactMap;
    private GameHandler myGameHandler;

    /**
     * constructor
     */
    public InteractionFactory () {
        super();
    }

    /**
     * loads the reaction hashMap from a file and sets
     * the reactionMap of the passed interaction
     * 
     * @param interaction
     * @param levelFile
     * @param gh
     * @return
     * @throws ClassNotFoundException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public void setReactionMap (Interaction interaction, File levelFile,
            GameHandler gh) {
        myReactMap = new HashMap<SpritesTypePair, Reaction>();
        myGameHandler = gh;
        loadObjects(levelFile);
        interaction.registerMultipleReactions(myReactMap);
    }

    @Override
    protected void addObject (Scanner line) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException {
        String className1 = BEHAVIOR_LOCATION + line.next();
        Class aClass1 = Class.forName(className1);
        Behavior behaviorID1 = (Behavior) aClass1.newInstance();
        behaviorID1.setGameHandler(myGameHandler);

        String className2 = BEHAVIOR_LOCATION + line.next();
        Class aClass2 = Class.forName(className2);
        Behavior behaviorID2 = (Behavior) aClass2.newInstance();
        behaviorID2.setGameHandler(myGameHandler);

        String className3 = REACTION_LOCATION + line.next();
        Class aClass3 = Class.forName(className3);
        Reaction newReaction = (Reaction) aClass3.newInstance();

        SpritesTypePair typePairID = new SpritesTypePair(
                behaviorID1.getClass(), behaviorID2.getClass());
        myReactMap.put(typePairID, newReaction);
    }

}
